import Vue from 'vue';
import Vuex from 'vuex';
import clone from '@/lib/clone';
import createId from '@/lib/createId.ts';
import router from '@/router'

Vue.use(Vuex);
// 把 store 绑到 Vue.prototype.$store = store



const store = new Vuex.Store({
    state: {
        recordList: [] as RecordItem[],
        tagList: [] as Tag[],
        currentTag: undefined,
        createRecordError: null,
        createTagError: null,
    } as RootState,
    mutations: {

        fetchRecords(state) {
            state.recordList = JSON.parse(window.localStorage.getItem('recordList') || '[]') as RecordItem[];
        },
        createRecords(state, record) {
            const record2: RecordItem = clone(record);
            state.recordList.push(record2);
            store.commit('saveRecords');
        },
        saveRecords(state) {
            window.localStorage.setItem('recordList', JSON.stringify(state.recordList));
        },
        /**/
        fetchTags(state) {
            state.tagList = JSON.parse(window.localStorage.getItem('tagList') || '[]');
            if(!state.tagList || state.tagList.length ===0){
                store.commit('createTag','衣')
                store.commit('createTag','食')
                store.commit('createTag','住')
                store.commit('createTag','行')

            }
        },
        setCurrentTag(state, id: string) {
            state.currentTag = state.tagList.filter(t => t.id === id)[0];
        }
        ,
        createTag(state, name: string) {
            state.createTagError = null;
            const names = state.tagList.map(item => item.name);
            if (names.indexOf(name) > 0) {
                state.createTagError = new Error('tag name duplicated');
                return;

            }
            //查找重复的值，找到以后返回'duplicated'表示重复
            const id = createId().toString();
            state.tagList.push({id: id, name: name});
            store.commit('saveTags');
        },

        updateTag(state, payload: { id: string, name: string }) {
            const idList = state.tagList.map(item => item.id);
            const id = payload.id;
            const name = payload.name;
            //确定id是否在数据中
            console.log('idList', idList);
            if (idList.indexOf(id) >= 0) {
                const names = state.tagList.map(item => item.name);
                //确定用户输入的标签是否存在于数据中

                if (names.indexOf(name) >= 0) {
                    console.log('重复', name);
                    return 'duplicated';
                    //    表示重复
                } else {
                    const tag = state.tagList.filter(item => item.id === id)[0];
                    tag.name = name;
                    store.commit('saveTags');
                    console.log('成功');
                    return 'success';
                }
            } else {
                console.log('找不到');
                return 'not found';
            }
        },
        removeTag(state,id: string) {
            const idList = state.tagList.map(item => item.id);
            const index = idList.indexOf(id);
            if (index >= 0) {
                state.tagList.splice(index, 1);
                store.commit('saveTags');
                router.replace('/labels');
                window.alert('删除成功')
            } else {
                window.alert('删除失败')
            }
        },
        saveTags(state) {
            window.localStorage.setItem('tagList', JSON.stringify(state.tagList));
            //发生未知情况，续上
        },
    },
    actions: {},
    modules: {}
});
export default store;